/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.inventorycheckdetail.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fhs.core.trans.anno.Trans;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.inventorycheck.entity.BizInventoryCheck;
import vip.xiaonuo.biz.modular.inventorycheck.mapper.BizInventoryCheckMapper;
import vip.xiaonuo.biz.modular.inventorycheckdetail.entity.BizInventoryCheckDetail;
import vip.xiaonuo.biz.modular.inventorycheckdetail.mapper.BizInventoryCheckDetailMapper;
import vip.xiaonuo.biz.modular.inventorycheckdetail.param.BizInventoryCheckDetailAddParam;
import vip.xiaonuo.biz.modular.inventorycheckdetail.param.BizInventoryCheckDetailEditParam;
import vip.xiaonuo.biz.modular.inventorycheckdetail.param.BizInventoryCheckDetailIdParam;
import vip.xiaonuo.biz.modular.inventorycheckdetail.param.BizInventoryCheckDetailPageParam;
import vip.xiaonuo.biz.modular.inventorycheckdetail.service.BizInventoryCheckDetailService;
import vip.xiaonuo.biz.modular.inventorycheckdetail.vo.BizInventoryCheckDetailVO;
import vip.xiaonuo.biz.modular.cabinetdoor.entity.BizCabinetDoor;
import vip.xiaonuo.biz.modular.cabinetdoor.mapper.BizCabinetDoorMapper;
import vip.xiaonuo.biz.modular.cabinetgroup.entity.BizCabinetGroup;
import vip.xiaonuo.biz.modular.cabinetgroup.mapper.BizCabinetGroupMapper;
import vip.xiaonuo.biz.modular.cabinetinfo.entity.BizCabinetInfo;
import vip.xiaonuo.biz.modular.cabinetinfo.mapper.BizCabinetInfoMapper;
import vip.xiaonuo.biz.modular.goods.entity.BizGoods;
import vip.xiaonuo.biz.modular.goods.mapper.BizGoodsMapper;
import vip.xiaonuo.common.exception.CommonException;

import java.util.List;

/**
 * 扫码盘库明细Service接口实现类
 *
 * @author Jonathan
 * @date 2023/11/07 11:34
 **/
@Service
public class BizInventoryCheckDetailServiceImpl extends ServiceImpl<BizInventoryCheckDetailMapper, BizInventoryCheckDetail> implements BizInventoryCheckDetailService {
    @Autowired
    private BizInventoryCheckMapper bizInventoryCheckMapper;
    @Autowired
    private BizGoodsMapper bizGoodsMapper;
    @Autowired
    private BizCabinetGroupMapper bizCabinetGroupMapper;
    @Autowired
    private BizCabinetInfoMapper bizCabinetInfoMapper;
    @Autowired
    private BizCabinetDoorMapper bizCabinetDoorMapper;

    @Override
    public Page<BizInventoryCheckDetailVO> page(BizInventoryCheckDetailPageParam param) {
        return getBaseMapper().page(Page.of(param.getCurrent(), param.getSize()), param);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BizInventoryCheckDetailAddParam param) {
        String scanCode = param.getScanCode();
        Assert.notEmpty(scanCode, "条码或二维码不能为空");

        BizInventoryCheck bizInventoryCheck = bizInventoryCheckMapper.selectById(param.getInventoryCheckId());
        Assert.notNull(bizInventoryCheck, String.format("盘库记录id=%s不存在", param.getInventoryCheckId()));

        String code = scanCode.split("_")[0];
        BizGoods bizGoods = bizGoodsMapper.selectOne(new LambdaQueryWrapper<BizGoods>().eq(BizGoods::getQrCode, code).last("limit 1"));
        Assert.notNull(bizGoods, "物品不在系统中");

        long existCount = count(new LambdaQueryWrapper<BizInventoryCheckDetail>()
                .eq(BizInventoryCheckDetail::getInventoryCheckId, param.getInventoryCheckId())
                .eq(BizInventoryCheckDetail::getGoodsId, bizGoods.getId()));
        Assert.isTrue(existCount == 0, "请勿重复添加物品");

        BizInventoryCheckDetail detail = new BizInventoryCheckDetail();
        detail.setInventoryCheckId(param.getInventoryCheckId());
        detail.setGoodsId(bizGoods.getId());
        detail.setGoodsName(bizGoods.getGoodsName());
        detail.setGoodsCode(bizGoods.getGoodsCode());
        detail.setGoodsImg(bizGoods.getGoodsImg());
        detail.setBioCabinetGroupId(bizGoods.getBioCabinetGroupId());
        if (StringUtils.isNotEmpty(bizGoods.getBioCabinetGroupId())) {
            BizCabinetGroup bizCabinetGroup = bizCabinetGroupMapper.selectById(bizGoods.getBioCabinetGroupId());
            if (bizCabinetGroup != null) detail.setBioCabinetGroupName(bizCabinetGroup.getCabinetGroupName());
        }
        detail.setBioCabinetId(bizGoods.getBioCabinetId());
        if (StringUtils.isNotEmpty(bizGoods.getBioCabinetId())) {
            BizCabinetInfo bizCabinetInfo = bizCabinetInfoMapper.selectById(bizGoods.getBioCabinetId());
            if (bizCabinetInfo != null) detail.setBioCabinetName(bizCabinetInfo.getCabinetName());
        }
        detail.setBioCabinetDoorId(bizGoods.getBioCabinetDoorId());
        if (StringUtils.isNotEmpty(bizGoods.getBioCabinetDoorId())) {
            BizCabinetDoor bizCabinetDoor = bizCabinetDoorMapper.selectById(bizGoods.getBioCabinetDoorId());
            if (bizCabinetDoor != null) detail.setBioCabinetDoorName(bizCabinetDoor.getCabinetDoorName());
        }
        detail.setState(bizGoods.getState());
        detail.setLeaveFlag(bizGoods.getLeaveFlag());
        // 如果扫码盘选择的柜门 与 物品理论所在柜门不一致，就是放错
        if (StringUtils.isNotEmpty(bizInventoryCheck.getCabinetDoorId())) {
            detail.setConsistentStorage(bizInventoryCheck.getCabinetDoorId().equals(bizGoods.getBioCabinetDoorId()) ? 1 : 0);
        } else if (StringUtils.isNotEmpty(bizInventoryCheck.getCabinetInfoId())) {
            detail.setConsistentStorage(bizInventoryCheck.getCabinetInfoId().equals(bizGoods.getBioCabinetId()) ? 1 : 0);
        }

        // 设置试剂的容量，重量
        detail.setReagentAvlVolume(bizGoods.getReagentAvlVolume());
        detail.setReagentAvlWeight(bizGoods.getReagentAvlWeight());
        detail.setReagentCasNo(bizGoods.getReagentCasNo());
        this.save(detail);
        // 修改数量
        BizInventoryCheck updateBean = new BizInventoryCheck();
        updateBean.setId(param.getInventoryCheckId());
        // get count via inventoryCheckId
        long chkInventory = count(new LambdaQueryWrapper<BizInventoryCheckDetail>().eq(BizInventoryCheckDetail::getInventoryCheckId, param.getInventoryCheckId()));
        updateBean.setChkInventory(chkInventory);
        bizInventoryCheckMapper.updateById(updateBean);
    }

    @Override
    public void edit(BizInventoryCheckDetailEditParam bizInventoryCheckDetailEditParam) {
        BizInventoryCheckDetail bizInventoryCheckDetail = this.queryEntity(bizInventoryCheckDetailEditParam.getId());
        BeanUtil.copyProperties(bizInventoryCheckDetailEditParam, bizInventoryCheckDetail);
        this.updateById(bizInventoryCheckDetail);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<BizInventoryCheckDetailIdParam> param) {
        Assert.notEmpty(param, "请求参数不能为空");
        String id = param.get(0).getId();
        BizInventoryCheckDetail bizInventoryCheckDetail = getBaseMapper().selectById(id);
        String inventoryCheckId = bizInventoryCheckDetail.getInventoryCheckId();
        // 执行删除
        this.removeBatchByIds(CollStreamUtil.toList(param, BizInventoryCheckDetailIdParam::getId));
        // get count via inventoryCheckId
        Long chkInventory = count(new LambdaQueryWrapper<BizInventoryCheckDetail>().eq(BizInventoryCheckDetail::getInventoryCheckId, inventoryCheckId));
        // update count
        BizInventoryCheck updateBean = new BizInventoryCheck();
        updateBean.setId(inventoryCheckId);
        updateBean.setChkInventory(chkInventory);
        bizInventoryCheckMapper.updateById(updateBean);
    }

    @Override
    public BizInventoryCheckDetail detail(BizInventoryCheckDetailIdParam bizInventoryCheckDetailIdParam) {
        return this.queryEntity(bizInventoryCheckDetailIdParam.getId());
    }

    @Override
    public BizInventoryCheckDetail queryEntity(String id) {
        BizInventoryCheckDetail bizInventoryCheckDetail = this.getById(id);
        if (ObjectUtil.isEmpty(bizInventoryCheckDetail)) {
            throw new CommonException("扫码盘库明细不存在，id值为：{}", id);
        }
        return bizInventoryCheckDetail;
    }
}
